package com.wanmait.recruit.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.mapper.CityMapper;
import com.wanmait.recruit.pojo.*;
import com.wanmait.recruit.service.*;
import com.wanmait.recruit.util.PageContext;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

@Controller
@RequestMapping("/web/company")
public class CompanyController {

    @Resource
    CompanyService companyService;

    @Resource
    CompanyAdminService companyAdminService;

    @Resource
    CompanyAuthcService companyAuthcService;

    @Resource
    CompanyHrService companyHrService;

    @Resource
    CityMapper cityMapper;

    @Resource
    CompanyPositionService companyPositionService;

    @RequestMapping("/companyListData")
    public ModelAndView companyListData(String companyName,String city,String scope){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/company/companyListData");
        Integer scopeMin = null;
        Integer scopeMax = null;
        switch (scope){
            case "0":
                break;
            case "1":
                scopeMax=10;
                scopeMin=0;
                break;
            case "2":
                scopeMax=50;
                scopeMin=10;
                break;
            case "3":
                scopeMax=200;
                scopeMin=50;
                break;
            case "4":
                scopeMax=500;
                scopeMin=200;
                break;
            case "5":
                scopeMax=2000;
                scopeMin=500;
                break;
            case "6":
                scopeMin=2000;
                break;
        }
        PageInfo<Company> pageInfo = companyService.search(PageContext.getPageNum(),9,companyName,city,scopeMin,scopeMax);
        mv.addObject(pageInfo);

        return mv;
    }

    @RequestMapping("/companyList")
    public ModelAndView companyList(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/company/companyList");
        PageInfo<Company> pageInfo = companyService.search(PageContext.getPageNum(),9);
        mv.addObject(pageInfo);

        return mv;
    }

    @RequestMapping("/manage/companyUpdate")
    public String manageCompanyUpdate(Company company,MultipartFile file){
        //System.out.println(company.toString());
        //公司logo
        if(!file.isEmpty()) {
            //物理路径companyLicenseImages
            String realpath = "C:\\Users\\Administrator\\IdeaProjects\\myRecruit\\src\\main\\webapp\\assets\\images/companies";
            // 原文件名
            String originalFileName = file.getOriginalFilename();
            // 获得扩展名
            String ext = FilenameUtils.getExtension(originalFileName);
            // 唯一文件名
            String fileName = UUID.randomUUID() + "." + ext;
            File outFile = new File(realpath, fileName);
            // 保存
            try
            {
                file.transferTo(outFile);
                company.setLogo(fileName);
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        companyService.update(company);
        return "redirect:/web/company/manage/companyInfo";
    }

    @RequestMapping("/manage/checkCompanyName")
    public void manageCheckCompanyName(String companyName, HttpServletResponse response)throws IOException{
        Company company = companyService.findByCompanyName(companyName);
        PrintWriter pw = response.getWriter();
        if(company!=null){
            pw.print(0);
        }
        else{
            pw.print(1);
        }
    }

    @PostMapping("/manage/companySign")
    public String manageCompanySign(Company company, MultipartFile file1,MultipartFile file2, HttpServletRequest request){

        HttpSession session = request.getSession();
        //公司logo
        if(!file2.isEmpty()) {
            //物理路径companyLicenseImages
            String realpath = "C:\\Users\\Administrator\\IdeaProjects\\myRecruit\\src\\main\\webapp\\assets\\images/companies";
            // 原文件名
            String originalFileName = file2.getOriginalFilename();
            // 获得扩展名
            String ext = FilenameUtils.getExtension(originalFileName);
            // 唯一文件名
            String fileName = UUID.randomUUID() + "." + ext;
            File outFile = new File(realpath, fileName);
            // 保存
            try
            {
                file2.transferTo(outFile);
                company.setLogo(fileName);
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        String address = company.getAddress();
        int start = 0;
        if(address.indexOf("省")>0){
            start = address.indexOf("省");
        }
        int end = 0;
        if(address.indexOf("市")>0){
            end = address.indexOf("市");
        }
        String cityName = address.substring((start+1),end);
        if(cityName.length()>=2){
            City city = cityMapper.findByCityName(cityName);
            if(city!=null){
                company.setCityId(city.getId());
            }
        }
        //添加公司信息
        companyService.insert(company);

        company = companyService.findByCompanyName(company.getCompanyName());

        CompanyAuthc companyAuthc = new CompanyAuthc();

        companyAuthc.setCompanyId(company.getId());
        companyAuthc.setCompanyName(company.getCompanyName());

        CompanyAdmin companyAdmin = new CompanyAdmin();
        companyAdmin.setCompanyId(company.getId());
        Person person = (Person)session.getAttribute("person");
        companyAdmin.setPersonId(person.getId());
        //添加公司管理员
        companyAdminService.insert(companyAdmin);
        //营业执照
        if(!file1.isEmpty()) {
            //物理路径companyLicenseImages
             String realpath = "C:\\Users\\Administrator\\IdeaProjects\\myRecruit\\src\\main\\webapp\\static/companyLicenseImages";
            // 原文件名
             String originalFileName = file1.getOriginalFilename();
            // 获得扩展名
             String ext = FilenameUtils.getExtension(originalFileName);
            // 唯一文件名
             String fileName = UUID.randomUUID() + "." + ext;
             File outFile = new File(realpath, fileName);
            // 保存
             try
             {
                 file1.transferTo(outFile);
                 companyAuthc.setLicensePic(fileName);
             } catch (IOException e)
             {
                 e.printStackTrace();
             }
        }
        //添加公司审核信息
        companyAuthcService.insert(companyAuthc);

        return "redirect:/web/index";
    }

    @GetMapping("/manage/companySign")
    public ModelAndView manageCompanySign(HttpServletRequest request){

        ModelAndView modelAndView = new ModelAndView();
        HttpSession session = request.getSession();
        Person person = (Person)session.getAttribute("person");
        if(person==null){
            modelAndView.setViewName("/web/company/manage/companySign");
            return modelAndView;
        }
        CompanyAdmin companyAdmin = companyAdminService.findByPerson(person);

        if(companyAdmin!=null){

            modelAndView.setViewName("redirect:/web/company/manage/companyInfo?msg=companyExist");
            return modelAndView;
        }
        modelAndView.setViewName("/web/company/manage/companySign");
        return modelAndView;

    }

    @RequestMapping("/manage/companyInfo")
    public ModelAndView manageCompanyInfo(HttpSession session){
        Person person = (Person)session.getAttribute("person");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/web/company/manage/companyInfo");
        if(person==null){
            return modelAndView;
        }
        CompanyAdmin companyAdmin = companyAdminService.findByPerson(person);
        if(companyAdmin!=null){
            Company company = companyAdmin.getCompany();
            CompanyAuthc companyAuthc = companyAuthcService.findByCompany(company);
            modelAndView.addObject(company);
            modelAndView.addObject(companyAuthc);
            person.setIsCompanyAdmin(true);
            session.setAttribute("person",person);
            return modelAndView;
        }
        CompanyHr companyHr = companyHrService.findByPerson(person);
        if(companyHr!=null){
            Company company = companyHr.getCompany();
            CompanyAuthc companyAuthc = companyAuthcService.findByCompany(company);
            modelAndView.addObject(company);
            modelAndView.addObject(companyAuthc);
            return modelAndView;
        }
        return modelAndView;
    }

    @RequestMapping("/position")
    public ModelAndView companyPosition(Integer companyId){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/company/position");
        Company company = new Company();
        company.setId(companyId);
        PageInfo<CompanyPosition> pageInfo = companyPositionService.findByCompany(company);
        mv.addObject(pageInfo);
        mv.addObject(company);
        return mv;
    }

    @RequestMapping("/companyInfo")
    public ModelAndView companyInfo(String companyName){
        System.out.println(companyName);

        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/company/companyInfo");
        Company company = companyService.findByCompanyName(companyName);
        System.out.println(company.toString());
        mv.addObject("company",company);
        CompanyAuthc companyAuthc = companyAuthcService.findByCompany(company);
        mv.addObject("companyAuthc",companyAuthc);
        PageInfo<CompanyPosition> pageInfo = companyPositionService.findByCompany(company);
        mv.addObject("pageInfo",pageInfo);


        return mv;
    }

}
